package com.supertange.miniapp.wepet.common.controller;

import com.qiniu.util.UrlSafeBase64;
import com.supertange.miniapp.wepet.common.service.RedisService;
import com.supertange.miniapp.wepet.common.utils.Assert;
import com.supertange.miniapp.wepet.common.utils.FileUploadUtil;
import com.supertange.miniapp.wepet.modules.wepet.entity.UsersEntity;
import com.supertange.miniapp.wepet.modules.wepet.entity.vo.UsersVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

/**
 * 控制器基类
 */
@Controller
public class BaseController {
    @Autowired
    public RedisService redisService;

    //七牛云
    @Value("${qiniu.accesskey}")
    public String ACCESSKEY;
    @Value("${qiniu.secretkey}")
    public String SECRETKEY;
    @Value("${qiniu.bucket}")
    public String BUCKET;
    @Value("${qiniu.host}")
    public String HOST;

    //上传文件的根路径
    @Value("${upload.root.path}")
    public String uploadRootPath;
    @Value("${wepet.waterImageUrl}")
    public String waterImageUrl;

    //redis的文件目录
    public static final String USER_REDIS_SESSION = "user-redis-session";

    // 每页分页的记录数
    public static final Integer PAGE_SIZE = 100;

    //默认第一页
    public static final Integer PAGE = 1;

    //redis设置用户session
    public UsersVO setUserRedisSessionToken(UsersEntity userModel) {
        String uniqueToken = UUID.randomUUID().toString();
        if (redisService.get(USER_REDIS_SESSION + ":" + userModel.getId()) != null)
            redisService.del(USER_REDIS_SESSION + ":" + userModel.getId());
        redisService.set(USER_REDIS_SESSION + ":" + userModel.getId(), uniqueToken, 1000 * 60 * 48);
        UsersVO userVO = new UsersVO();
        BeanUtils.copyProperties(userModel, userVO);
        userVO.setUserToken(uniqueToken);
        return userVO;
    }

    /**
     * 七牛文件上传
     *
     * @param file
     */
    @Transactional(rollbackFor = Exception.class)
    public String qnImageUpload(MultipartFile file) throws Exception {
        //取文件扩展名
        String fileName = file.getOriginalFilename();
        String extName = fileName.substring(fileName.lastIndexOf(".") + 1);
        //上传限制
        Assert.isTrue(("jpg".contains(extName) || "png".contains(extName)), "请按指定格式头像上传");
        String filePath = FileUploadUtil.qnUpload(file, ACCESSKEY, SECRETKEY, BUCKET, HOST, "image");
        return filePath;
    }


    /**
     * 七牛文件上传
     *
     * @param file
     */
    @Transactional(rollbackFor = Exception.class)
    public String qnVideoUpload(MultipartFile file) throws Exception {
        //取文件扩展名
        String fileName = file.getOriginalFilename();
        String extName = fileName.substring(fileName.lastIndexOf(".") + 1);
        //上传限制
        Assert.isTrue("mp4".contains(extName), "请按指定视频格式上传");
        String filePath = FileUploadUtil.qnUpload(file, ACCESSKEY, SECRETKEY, BUCKET, HOST, "video");
        return filePath;
    }

    /**
     *
     *视频截帧
     */
    @Transactional(rollbackFor = Exception.class)
    public String qnVideoPatchCover(String filePath) {
        if (StringUtils.isBlank(filePath)) Assert.isError("上传错误");
        filePath=filePath.substring(filePath.indexOf(".com/")+5);
        String coverPath=HOST+FileUploadUtil.qnPathCover(filePath,ACCESSKEY,SECRETKEY,BUCKET,"coverImage","jpg");
        return coverPath;
    }

    /**
     *
     *添加水印
     */
    @Transactional(rollbackFor = Exception.class)
    public String qnVideoAddWater(String filePath) {
        if (StringUtils.isBlank(filePath)) Assert.isError("上传错误");
        filePath=filePath.substring(filePath.indexOf(".com/")+5);
        String waterBase64Image= UrlSafeBase64.encodeToString(waterImageUrl);
        String waterVideo=HOST+ FileUploadUtil.qnVideoAddWater(filePath,ACCESSKEY,SECRETKEY,BUCKET,"video","mp4",waterBase64Image);
        return waterVideo;
    }

    /**
     * 七牛视频多个删除
     */
    public void qnDeleteFiles(String[] filePaths) {
        if (filePaths.length==0) Assert.isError("删除错误");
        for (int i=0;i<filePaths.length;i++){
            filePaths[i]=filePaths[i].substring(filePaths[i].indexOf(".com/")+5);
        }
        FileUploadUtil.qnDeleteVideo(filePaths,ACCESSKEY,SECRETKEY,BUCKET);
    }

}
